package com.spzx.product.controller;

import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.api.domain.Brand;
import com.spzx.product.service.BrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 分类品牌 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2024-10-26
 */
@Tag(name = "品牌")
@RestController
@RequestMapping("/brand")
public class BrandController extends BaseController {

    @Autowired
    private BrandService brandService;

    @Operation(summary = "查询品牌列表")
    @GetMapping("/list")
    public TableDataInfo getBrandList(String name){
        //开启分页
        startPage();
        //调用BrandService中带条件查询的方法
        List<Brand> brands = brandService.getBrandList(name);
        return getDataTable(brands);
    }

    @Operation(summary = "新增品牌")
    @RequiresPermissions("product:brand:add")
    @PostMapping
    public AjaxResult addBrand(@RequestBody @Validated Brand brand){
        //设置添加用户的名字
        brand.setCreateBy(SecurityUtils.getUsername());
        //调用brandService中的添加方法
        boolean save = brandService.save(brand);
        return toAjax(save);
    }

    /*
    * 回显
    * */
    @Operation(summary = "根据id查询品牌信息")
    @GetMapping("/{id}")
    public AjaxResult getBrandById(@PathVariable("id") Long id){
        Brand byId = brandService.getById(id);
        return success(byId);
    }

    @Operation(summary = "根据id更新品牌")
    @RequiresPermissions("product:brand:edit")
    @PutMapping
    public AjaxResult editBrand(@RequestBody @Validated Brand brand){
        boolean byId = brandService.updateById(brand);
        return toAjax(byId);
    }

    @Operation(summary = "根据id删除品牌")
    @DeleteMapping("/{id}")
    public AjaxResult deleteBrand(@PathVariable("id") List<Long> ids){
        //调用brandService中的方法批量删除
        boolean batchByIds = brandService.removeBatchByIds(ids);
        return toAjax(batchByIds);
    }

    @Operation(summary = "查询所有")
    @GetMapping("/getBrandAll")
    public AjaxResult getBrandAll(){
        //调用BrandService中带条件查询的方法
        List<Brand> brands = brandService.list();
        return AjaxResult.success(brands);
    }

    @InnerAuth
    @Operation(summary = "获取所有品牌")
    @GetMapping("/getBrandAllList")
    public R<List<Brand>> getAllBrand(){
        return R.ok(brandService.list());
    }



}
